using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SuedeTest.Core {
   public partial class ListViewDialog : Form {
      private bool _useIndex;
      private string _itemName;
      private bool _useName;
      public static readonly string SKIP_VALUE = "__SKIP__";

      public ListViewDialog() {
         InitializeComponent();
         //we can set names to emtpy string to hide them from SuedeTest
         this.Name = string.Empty;
         this._useIndex = true;
         this._useName = false;
      }

      public bool UseIndex {
         get {
            return _useIndex;
         }
      }

      public bool UseName {
         get {
            return _useName;
         }
      }

      public string ListItemName {
         get {
            return _itemName;
         }
      }

      public void Init(ListViewItem item) {
         lblFullname.Text = "";
         groupBox1.SuspendLayout();
         foreach (ListViewItem.ListViewSubItem subItem in item.SubItems) {
            CheckBox chkPartialName = new CheckBox();

            chkPartialName.AutoSize = true;
            chkPartialName.Text = subItem.Text;
            flowPartialName.Controls.Add(chkPartialName);

            lblFullname.Text += subItem.Text + " ";
         }
         lblFullname.Text.Trim();

         groupBox1.ResumeLayout();

         this.Width = groupBox1.Width + 5;
         this.Height = groupBox1.Height + 5;
      }

      private void rdoIndex_Click(object sender, EventArgs e) {
         _useIndex = true;
         _useName = true;
         _itemName = GenerateName();
         Close();
      }

      private void rdoFullName_Click(object sender, EventArgs e) {
         _useIndex = false;
         _useName = true;
         foreach (CheckBox chk in flowPartialName.Controls) {
            chk.Checked = true;
         }

         _itemName = GenerateName();
         Close();
      }

      private void rdoPartialName_Click(object sender, EventArgs e) {
         _useIndex = false;
         _useName = true;
         _itemName = GenerateName();
         Close();
      }

      private string GenerateName() {
         string partialName = "";
         foreach (CheckBox chk in flowPartialName.Controls) {
            if (chk.Checked) {
               partialName += chk.Text + Environment.NewLine;
            } else {
               partialName += SKIP_VALUE + Environment.NewLine;
            }
         }
         return partialName;
      }

      private void rdoIndexOnly_CheckedChanged(object sender, EventArgs e) {
         _useIndex = true;
         _useName = false;

         Close();
      }
   }
}